import datetime
import re
from datetime import datetime as dt

from lxml.html.diff import parse_html

from src.backend.entity.manager import Topic, Correcting
from src.backend.beanfactory import BeanFactory
from src.backend.tools.file import parser_html_to_strs


def topic_insert_sqlite(data):
    obs=[]
    for d in data:
        sql = """
               select id from topic where content=?
               """

        BeanFactory.getCursor().cursor.execute(sql, (d[0],))
        results = BeanFactory.getCursor().cursor.fetchall()
        if results:
            continue

        topic=Topic(d[0],d[1],d[2],d[3],d[4],d[5],d[6],d[7],d[8])
        obs.append(topic)
    if obs:
        BeanFactory.getCursor().insert(obs)
    return f'ok!{len(data)-len(obs)}重复，{len(obs)}入库'

def correct_insert_sqlite(data):
    obs=[]
    for d in data:
        sql="""
        select id from topic where text=?
        """

        BeanFactory.getCursor().cursor.execute(sql,(d[0],))
        results = BeanFactory.getCursor().cursor.fetchall()
        id=results[0][0]
        jj_correct=Correcting(id,'jj',dt.today().strftime('%Y-%m-%d'),d[1])
        uu_correct=Correcting(id,'uu',dt.today().strftime('%Y-%m-%d'),d[2])
        obs.append(jj_correct)
        obs.append(uu_correct)
    BeanFactory.getCursor().insert(obs)
    return 'ok'